//
//  MainHomeTableViewCell.m
//  ERP
//
//  Created by QA on 16/4/5.
//  Copyright © 2016年 ShangHai HaoLei Software Technology Co., Ltd. All rights reserved.
//

#import "MainHomeTableViewCell.h"
#import "NSDictionary+DataFormat.h"

#import "JZMTBtnView.h"
#import "ModuleAuthorityEntity+CoreDataClass.h"
#import "QueryMessageService.h"

#import "WebViewController.h"
#import "GEPushViewControllerHelper.h"
#import "MessageInfoEntity.h"
#import "JGProgressHUDHelper.h"

#define screen_width [UIScreen mainScreen].bounds.size.width
#define CellHeight SuperWidth == 375 ? 93 : 86

@interface MainHomeTableViewCell ()<UIScrollViewDelegate>
{
    UIView *_backView1;
    UIView *_backView2;
    UIPageControl *_pageControl;
    UINavigationController *navigationController;
    NSArray *menuValueArray;
}

@end

@implementation MainHomeTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(id)setMenuArray:(NSArray *)menuArray navigation:(UINavigationController*)navigation
{
    navigationController = navigation;
    menuValueArray = menuArray;
    
    _backView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screen_width, CellHeight)];
    [self addSubview:_backView1];
    
    //创建8个
    for (int i = 0; i < menuArray.count; i++) {
        if (i < 4) {
            ModuleAuthorityEntity *entity = [menuArray objectAtIndex:i];
            
            //是否可以点击
            CGRect frame = CGRectMake(i*screen_width/4, 0, screen_width/4, CellHeight);
            JZMTBtnView *btnView = [[JZMTBtnView alloc] initWithFrame:frame title:entity.title imageStr:entity.icon markBageNumber:0];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(OnTapBtnView:)];
            [btnView addGestureRecognizer:tap];
            
            btnView.tag = i;
            btnView.titleLable.textColor = [UIColorDispose colorWithHexString:@"#333333"];
            [_backView1 addSubview:btnView];
        }
    }
    
    return self;
}

-(void)OnTapBtnView:(UITapGestureRecognizer *)sender
{
    NSLog(@"tag:%ld",sender.view.tag);
    ModuleAuthorityEntity *entity = [menuValueArray objectAtIndex:sender.view.tag];
    if(!entity) {
        [JGProgressHUDHelper showErrorHUDText:@"无权限"];
        return;
    }
    
    if(entity.codeType.integerValue == 1){
        //网页模式
        WebPushParameterModel *model = [[WebPushParameterModel alloc] init];
        model.URL = entity.webCode;
        model.Title = entity.title;
        model.CodeType = CodeTypeWeb;
        
        WebViewController *viewController = [[WebViewController alloc] initWithPushParameterModel:model];
        [navigationController pushViewController:viewController animated:YES];
    } else {
        //原生APP模式
        [GEPushViewControllerHelper pushViewControllerHomePageCode:entity.nativeCode navigation:navigationController];
    }
}

//获取菜单权限信息
//- (ModuleAuthorityEntity*)getModuleAuthorityEntity:(id)idValue
//{
//    NSPredicate *menuPredicat = [NSPredicate predicateWithFormat:@"id = %@", idValue];
//    NSArray *menuArray = [ModuleAuthorityEntity MR_findAllSortedBy:@"order" ascending:YES withPredicate:menuPredicat];
//    if(menuArray.count <=0){
//        return nil;
//    } else {
//        return menuArray.firstObject;
//    }
//}

#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat scrollViewW = scrollView.frame.size.width;
    CGFloat x = scrollView.contentOffset.x;
    
    int page = (x + scrollViewW/2)/scrollViewW;
    _pageControl.currentPage = page;
}

@end
